package ipt.log;

import ipt.enumeracion.FormatoFecha;
import ipt.util.Util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Clase para guardar los registros o trazas de tiempos en la colecciones se
 * genera en una carpeta "data" dentro de la raiz del proyecto
 * 
 * FILE_NAME = 'registros.txt'
 * 
 * @author ur00
 * 
 */
public class Registros {

	private static final String FILE_PATH = "data/";
	private static final String FILE_NAME = "registros.txt";

	private FileWriter fw = null;
	private BufferedWriter bw = null;;
	private File file = null;

	private String user;

	private long timeIni;
	private long timeEnd;
	private long duracion;

	public Registros(long timeIni, long timeEnd, int numReg, String descripcion) {
		super();

		try {

			this.timeIni = timeIni;
			this.timeEnd = timeEnd;
			this.duracion = timeEnd - timeIni;
			this.user = Util.getUser();

			// conprombar que no sea nulo fichero
			if (file == null) {

				file = new File(FILE_PATH + FILE_NAME);
				fw = new FileWriter(file, true);
				bw = new BufferedWriter(fw);

				// Si no existe se crea nuevo
			} else if (!file.exists()) {
				file = new File(FILE_PATH + FILE_NAME);

			}

			fw = new FileWriter(file, true);
			bw = new BufferedWriter(fw);

			bw.write(this.user
					+ " , "
					+ Util.conversorFecha(System.currentTimeMillis())
					+ " , "
					+ Util.conversorFecha(this.timeIni)
					+ " , "
					+ Util.conversorFecha(this.timeEnd) + " , "
					+ String.valueOf(this.duracion) + " , " + numReg
					+ " registros" + " , " + descripcion);
			bw.newLine();

		} catch (Exception e) {
			System.out.println("Error en el fichero");
			e.printStackTrace();
		} finally {

			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					System.out.println("No se ha podido cerrar BufferedWriter");
					e.printStackTrace();
				}
			}
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					System.out.println("No se ha podido cerrar FileWriter");
					e.printStackTrace();
				}
			}
		}

	}

	
	/*
	public static void main(String[] args) throws Exception {
		
		
		
		/*
		Properties prop = System.getProperties();
		Enumeration enumProp = prop.elements();
		Enumeration enumKeys = prop.keys();
		
		while ( enumKeys.hasMoreElements() ){
			System.out.println( enumKeys.nextElement() );
=======

	public Registros(long timeIni, long timeEnd, String descripcion) {
		super();

		try {

			this.timeIni = timeIni;
			this.timeEnd = timeEnd;
			this.duracion = timeEnd - timeIni;
			this.user = Util.getUser();

			// conprombar que no sea nulo fichero
			if (file == null) {

				file = new File(FILE_PATH + FILE_NAME);
				fw = new FileWriter(file, true);
				bw = new BufferedWriter(fw);

				// Si no existe se crea nuevo
			} else if (!file.exists()) {
				file = new File(FILE_PATH + FILE_NAME);

			}

			fw = new FileWriter(file, true);
			bw = new BufferedWriter(fw);

			bw.write(this.user
					+ " , "
					+ Util.conversorFecha(System.currentTimeMillis())
					+ " , "
					+ Util.conversorFecha(this.timeIni)
					+ " , "
					+ Util.conversorFecha(this.timeEnd) + " , "
					+ String.valueOf(this.duracion) + " , " + descripcion);
			bw.newLine();

		} catch (Exception e) {
			System.out.println("Error en el fichero");
			e.printStackTrace();
		} finally {

			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					System.out.println("No se ha podido cerrar BufferedWriter");
					e.printStackTrace();
				}
			}
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					System.out.println("No se ha podido cerrar FileWriter");
					e.printStackTrace();
				}
			}
>>>>>>> .r447
		}
<<<<<<< .mine
		
		while ( enumProp.hasMoreElements() ){
			System.out.println(  enumProp.nextElement() );
		}	 
		
		
		System.out.println("Comenzamos prueba "+ System.getProperty("user") );
		for ( int i=0; i< 10; i++ ){ 
		 
			new Registros( System.currentTimeMillis(), System.currentTimeMillis() + i*1000 );
		}	
		System.out.println("Finalizamos prueba");
		
		private final static Logger log = Logger.getLogger("MICLASE".class);
		
		 log = Logger.getLogger("Logger de Ejemplo");
		
		log.info("Esto es un Log");
		*/


	/*
	 * public static void main(String[] args) throws Exception {
	 * 
	 * 
	 * Properties prop = System.getProperties(); Enumeration enumProp =
	 * prop.elements(); Enumeration enumKeys = prop.keys();
	 * 
	 * while ( enumKeys.hasMoreElements() ){ System.out.println(
	 * enumKeys.nextElement() ); }
	 * 
	 * while ( enumProp.hasMoreElements() ){ System.out.println(
	 * enumProp.nextElement() ); }
	 * 
	 * 
	 * System.out.println("Comenzamos prueba "+ System.getProperty("user") );
	 * for ( int i=0; i< 10; i++ ){
	 * 
	 * new Registros( System.currentTimeMillis(), System.currentTimeMillis() +
	 * i*1000 ); } System.out.println("Finalizamos prueba");
	 * 
	 * 
	 * }
	 */


}
